package com.contextor.cards.shell.core.apifactory
{
	import by.typing.vkontakte.ApplicationParameters;
	import by.typing.vkontakte.Vkontakte;
	import by.typing.vkontakte.commands.GetUserSettingsCommand;
	import by.typing.vkontakte.commands.IsAppUserCommand;
	import by.typing.vkontakte.data.Settings;
	import by.typing.vkontakte.data.SettingsData;
	import by.typing.vkontakte.events.SettingsEvent;
	import by.typing.vkontakte.events.VkontakteResponseEvent;
	import by.typing.vkontakte.events.WrapperEvent;
	import by.typing.vkontakte.net.VkontakteCall;
	
	import com.contextor.cards.shell.core.APIModel;
	import com.contextor.cards.shell.core.apievents.AppReadyToStartEvent;
	import com.contextor.cards.shell.core.apievents.InitAPIEvent;
	import com.contextor.cards.shell.core.apievents.IsAppUserEvent;
	
	import mx.controls.Alert;

	public class VKApiFactory
	{
		[Inject]
		public var model:APIModel;
		
		[MessageDispatcher]
		public var dispatcher:Function;
		
		public function VKApiFactory()
		{
		}
		
		
		[MessageHandler]
		public function initApi(event:InitAPIEvent):void
		{
			Vkontakte.getInstance().applicationParameters = model.wrapper.application.parameters;
			Vkontakte.getInstance().privateKey = model.privateKey;
			Vkontakte.getInstance().isTestMode = true;
			
			isAppUser(new IsAppUserEvent());
		}
		
		[MessageHandler]
		public function isAppUser(event:IsAppUserEvent):void
		{
			var isAppUser:IsAppUserCommand = new IsAppUserCommand(model.wrapper.application.parameters.viewer_id);
			isAppUser.addEventListener(VkontakteResponseEvent.RESPONSE, handleIsAppUser);
			 
			call(isAppUser);
		}
		
		private function handleIsAppUser(event:VkontakteResponseEvent):void
		{
			var isAppUser:Boolean = event.data as Boolean;
			
			if(isAppUser)
			{
				checkUserSettings();	
			}
			else 
			{
				model.wrapper.external.showInstallBox();
				model.wrapper.addEventListener(WrapperEvent.ON_APPLICATION_ADDED, handleAppAdded);
			}
		}
		
		private function handleAppAdded(event:WrapperEvent):void
		{
			isAppUser(new IsAppUserEvent());
		}
		
		private function checkUserSettings():void
		{
			var getUsetSettings:GetUserSettingsCommand = new GetUserSettingsCommand();
			getUsetSettings.addEventListener(VkontakteResponseEvent.RESPONSE, handleGetUserSettings);
			
			call(getUsetSettings);
		}
		
		private function handleGetUserSettings(event:VkontakteResponseEvent):void
		{
			var settings:SettingsData = event.data as SettingsData;
			
			if(settings.friendsEnabled)
			{
				model.settings = settings;
				
				
				dispatcher( new AppReadyToStartEvent() );
			}
			else 
			{
				showOurSettingsBox();
				model.wrapper.removeEventListener(SettingsEvent.ON_SETTINGS_CHANGED, handleSettingsChanged);
				model.wrapper.addEventListener(SettingsEvent.ON_SETTINGS_CHANGED, handleSettingsChanged);
			}
		}
		
		private function handleSettingsChanged(event:SettingsEvent):void
		{
			model.settings = event.settings;
			checkUserSettings();
		}
		
		public function showOurSettingsBox():void
		{
			model.wrapper.external.showSettingsBox(Settings.FRIENDS + Settings.PHOTOS)
		}
		
		private function call(vkCommand:VkontakteCall):void
		{
			Vkontakte.getInstance().call(vkCommand);
		}
	}
}